Search Query Parameter 本文未发布 发布文章

未匹配的标注

Below we’ll see how to setup a Search Query Parameter, on a Model:

  1. Add searchable Fields on the Model Repository, all the other steps are normal steps
<?php

namespace App\Containers\User\Data\Repositories;

use App\Containers\User\Contracts\UserRepositoryInterface;
use App\Ship\Parents\Repositories\Repository;

class UserRepository extends Repository implements UserRepositoryInterface
{

    protected $fieldSearchable = [
        'name'  => 'like',
        'id'    => '=',
        'email' => '=',
    ];

}

2.Create basic list and search Task

<?php

namespace App\Containers\User\Tasks;

use App\Containers\User\Contracts\UserRepositoryInterface;
use App\Port\Action\Abstracts\Action;

class ListUsersTask extends Action
{
    private $userRepository;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function run($order = true)
    {
        return $this->userRepository->paginate();
    }
}

3.Create basic Action to call that basic Task, and maybe other Tasks later in the future when needed

<?php

namespace App\Containers\User\Actions;

use App\Containers\User\Tasks\ListUsersTask;
use App\Port\Action\Abstracts\Action;

class ListAndSearchUsersAction extends Action
{

    private $listUsersTask;

    public function __construct(ListUsersTask $listUsersTask)
    {
        $this->listUsersTask = $listUsersTask;
    }

    public function run($order = true)
    {
        return $this->listUsersTask->run($order);
    }
} 

4.Use the Action from a Controller

<?php

public function listAllUsers()
{
    $users = Apiato::call('User@ListAndSearchUsersAction');

    return $this->response->paginator($users, new UserTransformer());
} 

5.Call it from anywhere as follow: [GET] http://api.apiato.com/users?search=Mahmoud@apiato.com

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~